

import 'package:flutter/cupertino.dart';
import 'package:visibility_detector/visibility_detector.dart';

class FMLifeVisibilityPage extends StatelessWidget {

  final BuildContext? parentContext;

  final String? customKey;

  final Widget? child;

  final Function()? viewWillAppear;

  final Function()? viewWillDisAppear;

  const FMLifeVisibilityPage({
    super.key,
    this.parentContext,
    required this.child,
    this.customKey,
    this.viewWillAppear,
    this.viewWillDisAppear,
  });

  @override
  Widget build(BuildContext context) {
    return VisibilityDetector(
        key: customKey != null ? Key(customKey!) : Key(parentContext!.widget.runtimeType.toString()),
        child: child!,
        onVisibilityChanged: (visibilityInfo){
          var visiblePercentage = visibilityInfo.visibleFraction * 100;
          if (visiblePercentage == 100) {
            if (viewWillAppear != null) {
              viewWillAppear!();
            }
          }else {
            if (viewWillDisAppear != null) {
              viewWillDisAppear!();
            }
          }
        }
    );
  }
}
